package com.study.spmongodb.book.wwj.char16;

/**
 * @author MI
 * @version 1.0
 * @date 2020/11/29 9:28
 */
public class EatNoodleThread extends Thread {
    private final String name;


    //左手边的餐具
    private final Tableware leftTool;

    private final Tableware rightTool;

    public EatNoodleThread(String name, Tableware leftToolName
            , Tableware rightTool) {
        this.leftTool = leftToolName;
        this.name = name;
        this.rightTool = rightTool;

    }

    @Override
    public void run() {
        while (true) {
            this.eat();
        }
    }

    private void eat() {
        synchronized (leftTool) {
            System.out.println(name + " take up " + leftTool + " {left} ");
            synchronized (rightTool) {
                System.out.println(name + " take up " + rightTool + " {right} ");
                System.out.println(name + " is eating now. ");
                System.out.println(name + " put down " + rightTool + " {right} ");
            }
            System.out.println(name + " put down " + leftTool + " {left} ");
        }
    }

    public static void main(String[] args) {
        final Tableware fork = new Tableware("fork");
        final Tableware knife = new Tableware("knife");

        new EatNoodleThread("A", fork, knife).start();
        new EatNoodleThread("B", fork, knife).start();
    }
}
